﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class LoginAndSignPanelManager : UIBase
{
    //开始显示的时间
    private float startTime = float.MaxValue;
    //显示连接失败
    private bool showConnFail = false;
    //ip和地址
    private string ip = "127.0.0.1";
    private int port = 8888;

    public GameObject signPanel;
    public GameObject loginPanel;
    public InputField loginIdInput;
    public InputField loginPwInput;
    public InputField signIdInput;
    public InputField signPwInput;
    // Start is called before the first frame update
    void Start()
    {
        LoginInit();
    }

    // Update is called once per frame
    void Update()
    {
        //连接失败
        if (showConnFail)
        {
            showConnFail = false;
            Debug.Log("链接失败");
            SetTipsPanel("网络连接失败，请重新启动游戏。");
            Invoke("QuitGame",2f);
        }
    }
    public void LoginInit() {
        //寻找组件
        //监听
        loginPanel.transform.GetChild(0).GetComponent<Button>().onClick.AddListener(OnLoginClick);
        signPanel.transform.GetChild(1).GetComponent<Button>().onClick.AddListener(OnSignClick);
        //网络协议监听
        NetManager.AddMsgListener("MsgLogin", OnMsgLogin);
        NetManager.AddMsgListener("MsgRegister", OnMsgRegister);
        //网络事件监听
        NetManager.AddEventListener(NetManager.NetEvent.ConnectSucc, OnConnectSucc);
        NetManager.AddEventListener(NetManager.NetEvent.ConnectFail, OnConnectFail);

        //连接服务器
        NetManager.Connect(ip, port);
        //记录时间
        startTime = Time.time;
    }
    //关闭
    public  void OnClose()
    {
        //网络协议监听
        NetManager.RemoveMsgListener("MsgLogin", OnMsgLogin);
        NetManager.RemoveMsgListener("MsgRegister", OnMsgRegister);
        //网络事件监听
        NetManager.RemoveEventListener(NetManager.NetEvent.ConnectSucc, OnConnectSucc);
        NetManager.RemoveEventListener(NetManager.NetEvent.ConnectFail, OnConnectFail);
    }
    //连接成功回调
    void OnConnectSucc(string err)
    {
        Debug.Log("OnConnectSucc");
    }
    //连接失败回调
    void OnConnectFail(string err)
    {
        showConnFail = true;
        //提示面板
        //PanelManager.Open<TipPanel>(err);
    }
    //当按下登陆按钮
    public void OnLoginClick()
    {
        //用户名密码为空
        if (loginIdInput.text == "" || loginPwInput.text == "")
        {
            SetTipsPanel("错误，用户名或密码不能为空。");
            return;
        }
        //发送
        MsgLogin msgLogin = new MsgLogin();
        msgLogin.id = loginIdInput.text;
        msgLogin.pw = loginPwInput.text;
        NetManager.Send(msgLogin);
    }
    public void OnSignClick() {
        //用户名密码为空
        if (signIdInput.text == "" || signPwInput.text == "")
        {
            PanelManager.Open<TipPanel>("用户名和密码不能为空");
            return;
        }
        //发送
        MsgRegister msgReg = new MsgRegister();
        msgReg.id = signIdInput.text;
        msgReg.pw = signIdInput.text;
        NetManager.Send(msgReg);
    }
    //收到登陆协议
    public void OnMsgLogin(MsgBase msgBase)
    {
        MsgLogin msg = (MsgLogin)msgBase;
        if (msg.result == 0)
        {
            //设置id
            GameMain.id = msg.id;
            //动效
            OnClose();
            loginPanel.transform.GetChild(0).GetComponent<Button>().interactable = false;
            signPanel.transform.GetChild(1).GetComponent<Button>().interactable = false;
            this.gameObject.GetComponent<StartLoginManager>().isConcc = true;
        }
        else
        {
            SetTipsPanel("登录失败，请检查用户名密码是否正确。");
        }
    }
    public void OnMsgRegister(MsgBase msgBase)
    {
        MsgRegister msg = (MsgRegister)msgBase;
        if (msg.result == 0)
        {
            Debug.Log("注册成功");
            //提示
            SetTipsPanel("注册成功。");
        }
        else
        {
            SetTipsPanel("该账号已经被注册过。");
        }
    }
}
